package com.juno.cafenoa.menu;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juno.cafenoa.R;
import com.juno.ui.uitableview.model.IListItem;
import com.juno.ui.uitableview.model.ViewItem;

public class MenuTableView extends LinearLayout {

	private int indexController = 0;
	public LayoutInflater rowInflater;
	public LinearLayout listContainer;
	private LinearLayout viewListContainer;
	private List<MenuBasicItem> menuList;
	private ClickListener mClickListener;
	private OnCheckedChangeListener checkedChangeListener;

	public MenuTableView(Context context, AttributeSet attrs) {
		super(context, attrs);
		menuList = new ArrayList<MenuBasicItem>();
		rowInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listContainer = (LinearLayout) rowInflater.inflate(
				R.layout.list_container, null);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		addView(listContainer, params);
		viewListContainer = (LinearLayout) listContainer
				.findViewById(R.id.buttonsContainer);
	}

	/**
	 * 
	 * @param productName
	 * @param summary
	 */
	public void addMenuBasicItem(String productName) {
		menuList.add(new MenuBasicItem(productName));
	}

	/**
	 * 
	 * @param productName
	 * @param productSummary
	 */
	public void addMenuBasicItem(String productName, String productSummary) {
		menuList.add(new MenuBasicItem(productName, productSummary));
	}

	public void addMenuBasicItem(String imgUrl, String productName, int price,
			boolean isAddedInCart) {
		menuList.add(new MenuBasicItem(imgUrl, productName, price,
				isAddedInCart));
	}

	/**
	 * 
	 * @param item
	 */
	public void addMenuBasicItem(MenuBasicItem item) {
		menuList.add(item);
	}

	/**
	 * 
	 * @param itemView
	 */
	public void addViewItem(MenuBasicItem itemView) {
		menuList.add(itemView);
	}

	public void commit() {
		indexController = 0;

		if (menuList.size() > 1) {
			// when the list has more than one item
			for (MenuBasicItem menuBasicItem : menuList) {
				View tempItemView;
				if (indexController == 0) {
					tempItemView = rowInflater.inflate(
							R.layout.order_detail_top, null);
				} else if (indexController == menuList.size() - 1) {
					tempItemView = rowInflater.inflate(
							R.layout.order_detail_bottom, null);
				} else {
					tempItemView = rowInflater.inflate(
							R.layout.order_detail_middle, null);
				}
				Log.d("OrderDetailTableView",
						"draw in commit()->obj.isSelectedProduct() "
								+ ((MenuBasicItem) menuBasicItem)
										.isAddedInCart());
				setupItem(tempItemView, menuBasicItem, indexController);
				tempItemView.setClickable(true);
				viewListContainer.addView(tempItemView);
				indexController++;
			}
		} else if (menuList.size() == 1) {
			// when the list has only one item
			View tempItemView = rowInflater.inflate(
					R.layout.order_detail_single, null);
			MenuBasicItem obj = menuList.get(0);
			setupItem(tempItemView, obj, indexController);
			tempItemView.setClickable(true);
			viewListContainer.addView(tempItemView);
		}
	}

	private void setupItem(View view, MenuBasicItem item, int index) {
		MenuBasicItem tempItem = (MenuBasicItem) item;
		setupBasicItem(view, tempItem, indexController);
	}

	/**
	 * 
	 * @param view
	 * @param item
	 * @param index
	 */
	private void setupBasicItem(View view, MenuBasicItem item, int index) {
		Log.d("OrderDetailTableView", "setupBasicItem() is called");
		// if (item.get() > -1) {
		Log.d("OrderDetailTableView", "item.getDrawable() is called");
		((ImageView) view.findViewById(R.id.image))
				.setBackgroundResource(R.drawable.user_image);
		// }
		if (item.getProductName() != null) {
			Log.d("OrderDetailTableView", "item.getProductName() is called");
			((TextView) view.findViewById(R.id.subtitle)).setText(item
					.getProductName());
		} else {
			Log.d("OrderDetailTableView", "else is called");
			((TextView) view.findViewById(R.id.subtitle))
					.setVisibility(View.GONE);
		}
		// 이전에 Cart에 추가한 Product라면 액티비티가 켜질때 체크됨
		Log.d("OrderDetailTableView",
				"item.isSelectedProduct() = " + item.isAddedInCart());

		// Check박스에 체크되면 처리할 리스너를 등록
		CheckBox cbChoosedProduct = (CheckBox) view
				.findViewById(R.id.cbChoosedProduct);
		cbChoosedProduct.setTag(index);
		cbChoosedProduct.setOnCheckedChangeListener(checkedChangeListener);
		cbChoosedProduct.setChecked(item.isAddedInCart());

		((TextView) view.findViewById(R.id.title)).setText(item
				.getProductName());

		view.setTag(index);

		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (mClickListener != null)
					mClickListener.onClick((Integer) view.getTag());
			}
		});
	}

	/**
	 * 
	 * @param view
	 * @param itemView
	 * @param index
	 */
	private void setupViewItem(View view, ViewItem itemView, int index) {
		if (itemView.getView() != null) {
			LinearLayout itemContainer = (LinearLayout) view
					.findViewById(R.id.itemContainer);
			itemContainer.removeAllViews();
			// itemContainer.removeAllViewsInLayout();
			itemContainer.addView(itemView.getView());

			if (itemView.isClickable()) {
				itemContainer.setTag(index);
				itemContainer.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						if (mClickListener != null)
							mClickListener.onClick((Integer) view.getTag());
					}
				});
			}
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getProductCountInMenu() {
		return menuList.size();
	}

	/**
	 * 
	 */
	public void clearMenuList() {
		menuList.clear();
		viewListContainer.removeAllViews();
	}

	public void removeProductInMenu(int index) {
		menuList.remove(index);
		viewListContainer.removeViewAt(index);
		// commit();
	}

	public interface ClickListener {
		void onClick(int index);
	}

	/**
	 * 
	 * @param listener
	 */
	public void setClickListener(ClickListener listener) {
		this.mClickListener = listener;
	}

	/**
	 * 
	 */
	public void removeClickListener() {
		this.mClickListener = null;
	}

	public LinearLayout getViewListContainer() {
		return viewListContainer;
	}

	public void setCheckedChangeListener(
			OnCheckedChangeListener checkedChangeListener) {
		this.checkedChangeListener = checkedChangeListener;
	}

	public MenuBasicItem getProductInMenu(int index) {
		return menuList.get(index);
	}

}